<?php
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 收藏模型控制器
 * 
 */
class CollectionController extends HomeController {

    public function index(){

    }

	public function collectionAdd(){
		//添加文章到收藏夹
		//post方式
		//article_id文章ID
		if(IS_POST){
			$json['success'] = true;
			$data = json_decode(file_get_contents('php://input'), true);
			if($data['article_id'] != null){
				$article_id = $data['article_id'];
			}else{
				$json['success'] = false;
				$json['error'] = '收藏文章找不到';
			}

			$uid = $this->login(); 

			if($uid){
				$data['uid'] = $uid;
				$data['article_id'] = $article_id;
				$IsHave = $this->checkCollectionIsHave($data);
				if(!$IsHave){
					$addResult = $this->AddCollection($data);
					if($addResult){
						$this->collectionNumAdd($article_id);
					}
				}else{
					if($IsHave['status']){
						$collect_data = M('collection')->where($data)->setDec('status');
						$article_data = M('articleshow')->where('id='.$article_id)->setDec('collect_num');
					}else{
						$collect_data = M('collection')->where($data)->setInc('status');
						$article_data = M('articleshow')->where('id='.$article_id)->setInc('collect_num');
					}
				}  
			}else{
				$json['success'] = false;
				$json['error'] = '用户未登录';
			}
			$this->ajaxReturn($json);
		}
		
	}

	public function collectionGet(){
		$uid = $this->login();
		$json['success'] = true;
		if($uid){
			$result = $this->getCollection($uid);
			$articleshowModel = D('articleshow');
			foreach ($result['info'] as $key => $value) {
				if($articleshowModel->articleShowGet($value)){
					$save_data[] = $articleshowModel->articleShowGet($value);
					
				}
			}
			
		}else{
			$json['success'] = false;
			$json['error'] = '用户未登录';
		}
		foreach ($save_data as $key => $value) {
            $banners = array();
            $artic =A('Articleshow');
           

             $banners =array(
                '0' => $artic->imgIdToPicture($value['0']['banner1']), 
                '1' => $artic->imgIdToPicture($value['0']['banner2']), 
                '2' => $artic->imgIdToPicture($value['0']['banner3']), 
                '3' => $artic->imgIdToPicture($value['0']['banner4']), 
                '4' => $artic->imgIdToPicture($value['0']['banner5']), 
                );
            $save_banners_data =array();
            foreach ($banners as $key2 => $value2) {
                if($value2){
                    $save_banners_data[] = $value2;  
                }   
            }
            if($save_banners_data){
                $save_data[$key]['banners'] = $save_banners_data;
            }else{
                $save_data[$key]['banners'] = null;
            }
            $save_data[$key]['0']['banners'] =$banners;
            
            
        }
		$json['info'] = $save_data;
		$this->ajaxReturn($json);
		

	}
	protected function checkCollectionIsHave($data){
		$collection = D('collection');
		$result = $collection->checkCollectionIsHave($data); 
		return $result;
	}
	
	protected function AddCollection($data){
		$collection = D('collection');
		$collectionresult = $collection->collectionAdd($data);
		return $collectionresult; 
	}

	protected function getCollection($uid){
		$collection = D('collection');
		$collectionresult = $collection->collectionGet($uid); 
		return $collectionresult;
	}

	protected function collectionNumAdd($article_id){
		$article = D('articleshow');
		$articleresult = $article->articleCollectNumAdd($article_id); 
	}
}
